package com.jinlejia.erp.ui.mine.contract;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.freedom.yefeng.yfrecyclerview.util.LogUtil;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivitySellProgressBinding;
import com.jinlejia.erp.ui.mine.adapter.SellProgressAdapter;
import com.jinlejia.erp.utils.DataFilter;
import com.jinlejia.erp.utils.LeftLineDecoration;
import com.jinlejia.erp.utils.RecommendState;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 销售进度
 */
public class SellProgressActivity extends BaseActivity {

    private SellProgressAdapter adapter;

    private ActivitySellProgressBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_sell_progress);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.addItemDecoration(new LeftLineDecoration(this));
        adapter = new SellProgressAdapter(new ArrayList<>());
        binding.recyclerView.setAdapter(adapter);
        binding.backBtn.setOnClickListener(view -> finish());
        String contractCode = getIntent().getStringExtra("contractCode");
        getData(contractCode);
    }


    private void getData(String contractCode) {
        OkHttpUtils.get()
                .url(ApiConfig.SELL_PROGRESS)
                .addParams("contractCode", contractCode)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        LogUtil.e("------销售进度----->", response.toString());
                        if (DataFilter.filter(SellProgressActivity.this, response, adapter)) {
                            JSONArray data = JSON.parseArray(response.get("data").toString());
                            ArrayList<Map<String, String>> dataList = new ArrayList<>();
                            for (int i = 0, j = data.size(); i < j; i++) {
                                JSONObject object = (JSONObject) data.get(i);
                                Map<String, String> map = new HashMap<>();
                                if (object.containsKey("createTime"))
                                    map.put("time", object.get("createTime").toString());
                                if (object.containsKey("resourceState"))
                                    map.put("state", RecommendState.getStateStr(object.getString("resourceState").toString()));
                                if (object.containsKey("resourceState"))
                                    map.put("resourceState", object.get("resourceState").toString());
                                if (object.containsKey("remark"))
                                    map.put("remark", object.get("remark").toString());
                                dataList.add(map);
                            }
                            Collections.sort(dataList, (lhs, rhs) -> (rhs.get("time").toString().compareTo(lhs.get("time").toString())));
                            adapter.setData(dataList);

                        }
                    }
                });
    }
}
